﻿using Microsoft.Extensions.Logging;
using NuGet.Protocol.Core.Types;
using QuickGet.Core.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickGet.Core
{
    public record GetPackageMetadataCommand(
        string PackageId, 
        string PackageVersion, 
        string PackageSourceName,
        bool IncludePrerelease,
        bool IncludeUnlisted)
    {
    }

    public class GetPackageMetadata : NugetOperationBase
    {
        public GetPackageMetadata(ILogger<GetPackageMetadata> logger, SourceCacheContext sourceCache, INugetResourceResolver nugetResourceResolver, INugetSourceRepositoryResolver nugetSourceRepositoryResolver) : base(logger, sourceCache, nugetResourceResolver, nugetSourceRepositoryResolver)
        {
        }

        public async Task<List<IPackageSearchMetadata>> ExecuteAsync(GetPackageMetadataCommand command, CancellationToken cancellationToken)
        {
            var resource = await _resourceResolver.GetResourceAsync<PackageMetadataResource>(command.PackageSourceName, cancellationToken);

            if(resource is null) { return []; }

            var versions = await resource.GetMetadataAsync(
                command.PackageId,
                command.IncludePrerelease,
                command.IncludeUnlisted,
                _sourceCache,
                _nugetLogger,
                cancellationToken);

            return versions.ToList();
        }
    }
}
